<?php
/**
 * 
 * @author chenqi
 * @uses 订单
 * @date 2015-07-17
 *
 */
class System_zhuan extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
		$this->load->model('system_money_zhuan_log_model');
                $this->load->model('user_model');
	}
        public function index() {
            $system_money_log_list=$this->system_money_zhuan_log_model->getSystemMoneyZhuanLogAll(array('from_user_id'=>$this->user['id']));
            $users=array();
            $result=array();
            
            foreach($system_money_log_list as $key=>$value){
                if(isset($users[$value['from_user_id']])){
                    $value['from_true_name']=$users[$value['from_user_id']]['true_name'];
                }
                $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                $user_info=$this->user_model->checkUser(array('id'=>$value['from_user_id']),'true_name');
                if($user_info){
                    $value['from_true_name']=$user_info['true_name'];
                    $users[$value['from_user_id']]['true_name']=$user_info['true_name'];
                }
                if(isset($users[$value['to_user_id']])){
                    $value['true_name']=$users[$value['to_user_id']]['true_name'];
                }
                $user_info=$this->user_model->checkUser(array('id'=>$value['to_user_id']),'true_name');
                if($user_info){
                    $value['to_true_name']=$user_info['true_name'];
                    $users[$value['to_user_id']]['true_name']=$user_info['true_name'];
                }
                $value['kou']=$value['kou']=='system'?'报单币':'余额';
                //$value['operate']=$shenhe_url;
                $result[]=$value;
            }
            $this->data['site_title']='转账记录';
            $this->data['system_zhuan']=$result;
            $this->load->view('/wap/historical_transfer',$this->data);
        }
    /**
     *转账
     */
    public function add_page(){
        $this->data['site_title']='转账';
        $this->load->model('user_model');
        $user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
        $this->data['user']=$user;

        $this->load->view('/wap/i_transfer',$this->data);
    }
    /**
     * 添加申请提现记录
     */
    public function add(){
        $message=false;
        $is_success=false;
        $_POST['username']=$this->user['username'];
        if(empty($_POST['username']) || empty($_POST['password'])){
            $message='参数错误';
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'id,username,password2,true_name,money,freeze,balance,system_money,system_balance');
            if(empty($user)){
                $message='用户不存在';
            }else if(($user['system_balance']-$_POST['money'])<0){
				 $message='余额不足';
			}else{
                if($user['password2']==md5($_POST['password'])){
                    $touser=$this->user_model->checkUser(array('user.id'=>$_POST['to_user_id']),'id,username,true_name,money,freeze,balance,system_money,system_balance');
                    //$tax=$_POST['money']*0.08;
                    $feiyong=$_POST['money']*ZHUANZHANG;
                    $data['from_user_id']=$user['id'];
                    $data['to_user_id']  =$_POST['to_user_id'];
                    $data['money']  =$_POST['money'];
                    $data['feiyong'] =$feiyong;
                    $data['shiji_money']=$_POST['money']-$feiyong;
                    $data['remark']=$_POST['remark'];
                    $data['kou']    =$_POST['kou'];
                    $data['remark']=$_POST['remark'];
                    $data['create_time']=time();
                    
                    $result=$this->system_money_zhuan_log_model->addSystemMoneyZhuanLog($data);
                    if($result){
                        $update_data['freeze']=$user['freeze']+$_POST['money'];
						$to_user_data=array();
                        if($_POST['kou']=='system'){
                            $update_data['system_balance']=$user['system_balance']-$_POST['money'];
							$to_user_data['system_balance']=$touser['system_balance']+$_POST['money'];
                        }
						/*else{
                            $update_data['balance']=$user['balance']-$_POST['money'];
							$to_user_data['balance']=$touser['balance']+$_POST['money'];
                        }*/
                        $update=$this->user_model->editUser(array('id'=>$user['id']),$update_data);
                        
                        
                        $update=$this->user_model->editUser(array('id'=>$_POST['to_user_id']),$to_user_data);
                        $update=true;
                        if($update){
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>$_POST['money'],
                                'type' =>'zhuan',
                                'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."给".$touser['username'].'转账'.$_POST['money']."元,扣除费用".$feiyong.'实际到账'.$data['shiji_money'],
                                'create_time'=>  time()
                            );
                            $logdata[]=array(
                                'user_id'=>$touser['id'],
                                'username'=>$touser['username'],
                                'true_name'=>$touser['true_name'],
                                'money'=>$_POST['money'],
                                'type' =>'zhuan',
                                'remark'=>$touser['username'].'您收到一笔来自'.$user['username'].'的转账，金额是'.$_POST['money'].'元。备注'.$_POST['remark'],
                                'create_time'=>  time()
                            );
                            $this->add_money_log($logdata);
                            $is_success=true;
                        }else{
                            $this->system_money_zhuan_log_model->deleteSystemMoneyZhuanLog(array('id'=>$result));
                            $message='添加失败';
                        }
                    }
                }else{
                    $message='交易密码错误';
                }
            }
        }
        echo json_encode(array('success'=>$is_success,'msg'=>$message));
    }
    /**
     * 检测用户是否正确
     */
    public function check_user(){
        $return =array();
        $return['success']='0';
        if($_POST['username']){
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']));
            if($user ){
                if($user['id']!=$_POST['from_user_id']){
                    $return['user']=$user;
                    $return['success']='1';
                }else{
                    $return['error']='不能给自己转账';
                }
            }else{
                $return['error']='用户不存在';
            }
        }else{
            $return['error']='用户不存在';
        }
        echo json_encode($return);
    }
}